import AJAX from '@/utils/ajax'
import starClass from '@/components/rating/style'

class Rating {
  constructor() {
    this.load()
    this.set('ul.rating li')
    this.hadpingfen = 0
  }
  load() {
    AJAX({ url: 'gold', data: { id: $('#rating-main').data('id'), sid: $('#rating-main').data('sid') } }).then(res => {
      this.show(res.gold)
    })
  }
  show(data) {
    console.log(data)
    let { a, b, c, d, e, pinfen, hadpingfen } = data['curpingfen']
    // const { mygold } = data
    a = +a
    b = +b
    c = +c
    d = +d
    e = +e

    $('#pa').html(a + '人')
    $('#pb').html(b + '人')
    $('#pc').html(c + '人')
    $('#pd').html(d + '人')
    $('#pe').html(e + '人')

    var totalnum = a + b + c + d + e
    var pam = ((a / totalnum) * 100).toFixed(1) + '%'
    var pbm = ((b / totalnum) * 100).toFixed(1) + '%'
    var pcm = ((c / totalnum) * 100).toFixed(1) + '%'
    var pdm = ((d / totalnum) * 100).toFixed(1) + '%'
    var pem = ((e / totalnum) * 100).toFixed(1) + '%'
    $('.d-juqing-star em').text(totalnum)
    if (totalnum > 0) {
      $('#pam').animate({ width: pam }, 500)
      $('#pbm').animate({ width: pbm }, 500)
      $('#pcm').animate({ width: pcm }, 500)
      $('#pdm').animate({ width: pdm }, 500)
      $('#pem').animate({ width: pem }, 500)
      $('#pam')
        .parent()
        .attr('title', pam)
      $('#pbm')
        .parent()
        .attr('title', pbm)
      $('#pcm')
        .parent()
        .attr('title', pcm)
      $('#pdm')
        .parent()
        .attr('title', pdm)
      $('#pem')
        .parent()
        .attr('title', pem)
    }

    var PFbai = pinfen * 10
    if (PFbai > 0 && totalnum > 0) {
      $('#rating-main').show()
      $('#rating-kong').hide()
      var pfnum = parseInt(PFbai) / 2
      $('#d-star').addClass(starClass(pfnum))
      if (pinfen === '10.0') {
        pinfen = '10'
      }
      $('#d-fen').text(pinfen)
    } else {
      $('#rating-main').hide()
      $('#rating-kong').show()
      $('.loadingg')
        .addClass('nopingfen')
        .html('暂无评分，赶快打分吧！')
    }
    // if (mygold > 0) {
    //   const curnum = mygold - 1
    //   const arr = new Array('很差', '较差', '还行', '推荐', '力荐')
    //   $('ul.rating li:lt(' + curnum + ')').addClass('current')
    //   $('ul.rating li:eq(' + curnum + ')').addClass('current')
    //   $('ul.rating li:gt(' + curnum + ')').removeClass('current')
    //   $('#ratewords').html(arr[curnum])
    // }
    if (hadpingfen != undefined && hadpingfen != null) {
      this.hadpingfen = 1
    }
  }
  set(obj) {
    const that = this
    $(obj).each(function(i) {
      const $title = $(this).attr('title')
      const num = $(this).index()
      const n = num + 1
      $(this)
        .click(function() {
          if (that.hadpingfen === 1) {
            layer.msg('已经评分,请务重复评分')
          } else {
            $(obj).removeClass('active')
            $(`${obj}:lt(${n})`).addClass('active')
            $('#ratewords').html($title)
            AJAX({ url: 'addgold', data: { val: $(this).attr('val'), id: $('#rating-main').data('id') } }).then(res => {
              if (res.code === -1) {
                that.hadpingfen = 1
              } else {
                that.load()
              }
              layer.msg(res.msg)
            })
          }
        })
        .hover(
          function() {
            $(this)
              .nextAll()
              .removeClass('active')
            $(this)
              .prevAll()
              .addClass('active')
            $(this).addClass('active')
            $('#ratewords').html($title)
          },
          function() {
            $(`${obj}:lt(${n})`).removeClass('active')
          }
        )
    })
  }
}

export default Rating
